using System.Configuration;

using Microsoft.Practices.CompositeWeb;
using Microsoft.Practices.CompositeWeb.Configuration;
using Microsoft.Practices.CompositeWeb.Interfaces;

using MVPComposibility.ContactManager.DomainModel;
using MVPComposibility.ContactManager.Services;
using MVPComposibility.ContactManager.Views.ContactList;

namespace MVPComposibility.ContactManager
{
	public class ContactManagerModuleInitializer : ModuleInitializer
	{
		private const string AuthorizationSection = "compositeWeb/authorization";

		public override void Load(CompositionContainer moduleContainer)
		{
			base.Load(moduleContainer);
			moduleContainer.RegisterTypeMapping<IContactListModel, ContactListModel>();
			AddGlobalServices(moduleContainer.Parent.Services);
			AddModuleServices(moduleContainer.Services);
			RegisterSiteMapInformation(moduleContainer.Services.Get<ISiteMapBuilderService>(true));
		}

		protected virtual void AddGlobalServices(IServiceCollection globalServices)
		{
			// TODO: add a service that will be visible to any module
		}

		protected virtual void AddModuleServices(IServiceCollection moduleServices)
		{
			Contact c1 = new Contact("Contact 1");
			c1.Id = 1;
			c1.AddAddress("home", "123 main street", "somewhere", "WA", "98000");
			Contact c2 = new Contact("Contact 2");
			c2.Id = 2;
			c2.AddAddress("work", "321 south main", "Somewhere ELse", "OR", "97000");

			ContactService service = new ContactService(new Contact[] { c1, c2 });

			moduleServices.Add(typeof(ContactService), service);
		}

		protected virtual void RegisterSiteMapInformation(ISiteMapBuilderService siteMapBuilderService)
		{
			SiteMapNodeInfo node = new SiteMapNodeInfo("ContactManager", "~/ContactManager/Default.aspx", "Contact Manager");
            siteMapBuilderService.AddNode(node);

            node = new SiteMapNodeInfo("AjaxContactManager", "~/ContactManager/AjaxContacts.aspx", "Ajax Contact Manager");
            siteMapBuilderService.AddNode(node);

            node = new SiteMapNodeInfo("AjaxContactManagerGenericMediator", "~/ContactManager/AjaxContactsGenericMediator.aspx", "Ajax Contact Manager Generic Mediator");
            siteMapBuilderService.AddNode(node);

            node = new SiteMapNodeInfo("AjaxContactManagerEventBroker", "~/ContactManager/AjaxContactsEventBroker.aspx", "Ajax Contact Manager Event Broker");
            siteMapBuilderService.AddNode(node);
        }

		public override void Configure(IServiceCollection services, Configuration moduleConfiguration)
		{
			IAuthorizationRulesService authorizationRuleService = services.Get<IAuthorizationRulesService>();
			if (authorizationRuleService != null)
			{
				AuthorizationConfigurationSection authorizationSection = moduleConfiguration.GetSection(AuthorizationSection) as AuthorizationConfigurationSection;
				if (authorizationSection != null)
				{
					foreach (AuthorizationRuleElement ruleElement in authorizationSection.ModuleRules)
					{
						authorizationRuleService.RegisterAuthorizationRule(ruleElement.AbsolutePath, ruleElement.RuleName);
					}
				}
			}
		}
	}
}